<template>
  <el-dialog
    :title="!dataForm.uid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户uid" prop="createUserUid">
      <el-input v-model="dataForm.createUserUid" placeholder="用户uid"></el-input>
    </el-form-item>
    <el-form-item label="访问ip地址" prop="ip">
      <el-input v-model="dataForm.ip" placeholder="访问ip地址"></el-input>
    </el-form-item>
    <el-form-item label="榜单成员uid" prop="memberUid">
      <el-input v-model="dataForm.memberUid" placeholder="榜单成员uid"></el-input>
    </el-form-item>
    <el-form-item label="附加数据(比如搜索内容)" prop="otherData">
      <el-input v-model="dataForm.otherData" placeholder="附加数据(比如搜索内容)"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="操作系统" prop="os">
      <el-input v-model="dataForm.os" placeholder="操作系统"></el-input>
    </el-form-item>
    <el-form-item label="浏览器" prop="browser">
      <el-input v-model="dataForm.browser" placeholder="浏览器"></el-input>
    </el-form-item>
    <el-form-item label="ip来源" prop="ipSource">
      <el-input v-model="dataForm.ipSource" placeholder="ip来源"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          uid: 0,
          createUserUid: '',
          ip: '',
          memberUid: '',
          otherData: '',
          status: '',
          createTime: '',
          updateTime: '',
          os: '',
          browser: '',
          ipSource: ''
        },
        dataRule: {
          createUserUid: [
            { required: true, message: '用户uid不能为空', trigger: 'blur' }
          ],
          ip: [
            { required: true, message: '访问ip地址不能为空', trigger: 'blur' }
          ],
          memberUid: [
            { required: true, message: '榜单成员uid不能为空', trigger: 'blur' }
          ],
          otherData: [
            { required: true, message: '附加数据(比如搜索内容)不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          os: [
            { required: true, message: '操作系统不能为空', trigger: 'blur' }
          ],
          browser: [
            { required: true, message: '浏览器不能为空', trigger: 'blur' }
          ],
          ipSource: [
            { required: true, message: 'ip来源不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.uid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.uid) {
            this.$http({
              url: this.$http.adornUrl(`/rank/membervisit/info/${this.dataForm.uid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.createUserUid = data.memberVisit.createUserUid
                this.dataForm.ip = data.memberVisit.ip
                this.dataForm.memberUid = data.memberVisit.memberUid
                this.dataForm.otherData = data.memberVisit.otherData
                this.dataForm.status = data.memberVisit.status
                this.dataForm.createTime = data.memberVisit.createTime
                this.dataForm.updateTime = data.memberVisit.updateTime
                this.dataForm.os = data.memberVisit.os
                this.dataForm.browser = data.memberVisit.browser
                this.dataForm.ipSource = data.memberVisit.ipSource
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/rank/membervisit/${!this.dataForm.uid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'uid': this.dataForm.uid || undefined,
                'createUserUid': this.dataForm.createUserUid,
                'ip': this.dataForm.ip,
                'memberUid': this.dataForm.memberUid,
                'otherData': this.dataForm.otherData,
                'status': this.dataForm.status,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'os': this.dataForm.os,
                'browser': this.dataForm.browser,
                'ipSource': this.dataForm.ipSource
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
